package com.tian.view.anim.attr_anim

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.tian.view.databinding.FragmentExpendViewUserAnimatorBinding
import kotlin.math.cos
import kotlin.math.sin

/**
 * @author tiansongtao, Email:tiansongtao@up366.com
 * @date 2022/12/29 9:53
 */
class UseAnimatorViewFragment : Fragment() {

    private lateinit var b: FragmentExpendViewUserAnimatorBinding
    private var expend = false
        set(value) {
            field = value
            if (field) expendView() else closeView()
        }

    private val radius = 400

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        b = FragmentExpendViewUserAnimatorBinding.inflate(inflater)
        return b.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        b.mbtRoot.setOnClickListener { expend = !expend }
    }

    private fun expendView() {
        doExpendAnimator(b.mbt1, 0, 5)
        doExpendAnimator(b.mbt2, 1, 5)
        doExpendAnimator(b.mbt3, 2, 5)
        doExpendAnimator(b.mbt4, 3, 5)
        doExpendAnimator(b.mbt5, 4, 5)
    }

    private fun doExpendAnimator(view: View, index: Int, total: Int) {

        view.visibility = View.VISIBLE
        val degree = Math.toRadians(90.0) / (total - 1) * index
        val translationX = -(radius * sin(degree)).toInt()
        val translationY = -(radius * cos(degree)).toInt()
        val set = AnimatorSet()
        set.playTogether(ObjectAnimator.ofFloat(view, "translationX", 0f, translationX.toFloat()),
                ObjectAnimator.ofFloat(view, "translationY", 0f, translationY.toFloat()),
                ObjectAnimator.ofFloat(view, "scaleX", 0f, 1f),
                ObjectAnimator.ofFloat(view, "scaleY", 0f, 1f),
                ObjectAnimator.ofFloat(view, "alpha", 0f, 1f),
                ObjectAnimator.ofFloat(view, "rotation", 0f, 360f))
        set.setDuration(500).start()
    }

    private fun closeView() {
        doCloseAnimator(b.mbt1, 0, 5)
        doCloseAnimator(b.mbt2, 1, 5)
        doCloseAnimator(b.mbt3, 2, 5)
        doCloseAnimator(b.mbt4, 3, 5)
        doCloseAnimator(b.mbt5, 4, 5)
    }

    private fun doCloseAnimator(view: View, index: Int, total: Int) {
        view.visibility = View.VISIBLE
        val degree = Math.toRadians(90.0) / (total - 1) * index
        val translationX = -(radius * sin(degree)).toInt()
        val translationY = -(radius * cos(degree)).toInt()
        val set = AnimatorSet()
        set.playTogether(ObjectAnimator.ofFloat(view, "translationX", view.translationX, 0f),
                ObjectAnimator.ofFloat(view, "translationY", view.translationY, 0f),
                ObjectAnimator.ofFloat(view, "scaleX", 1f, 0f),
                ObjectAnimator.ofFloat(view, "scaleY", 1f, 0f),
                ObjectAnimator.ofFloat(view, "alpha", 1f, 0f),
                ObjectAnimator.ofFloat(view, "rotation", 360f, 0f))
        set.setDuration(500).start()
    }
}